package com.lfd.threadpool.thread;

import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @项目名： spring-emq
 * @包名： com.lfd.threadpool.thread
 * @文件名: CustomThreadFactory
 * @创建者: liufudong@szyh-smart.com
 * @创建时间: 2024/10/12 14:25
 * @描述：
 */
public class CustomThreadFactory implements ThreadFactory {
    private final String namePrefix;
    private final AtomicInteger threadNumber = new AtomicInteger(1);
    private final boolean isDaemon;

    // 构造函数，允许设置线程名称前缀和是否为守护线程
    public CustomThreadFactory(final String namePrefix,final boolean isDaemon) {
        this.namePrefix = namePrefix;
        this.isDaemon = isDaemon;
    }
    @Override
    public Thread newThread(Runnable r) {
        Thread thread = new Thread(r);
        thread.setName(namePrefix + "-thread-" + threadNumber.getAndIncrement());
        thread.setDaemon(isDaemon);  // 设置是否为守护线程
        return thread;
    }
}
